package com.hyg.common.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.hyg.common.R;
/**
 * Author: huangyuguang
 * Date: 2022/5/6
 */
public abstract class BaseBottomDialog extends BottomSheetDialog {

    private BottomSheetBehavior mDialogBehavior;
    protected View rootView;

    public BaseBottomDialog(@NonNull Context context) {
        super(context, R.style.ActionSheetDialogStyle);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mDialogBehavior != null && mDialogBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
            mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rootView = LayoutInflater.from(getContext()).inflate(contentViewId(), null);
        setContentView(rootView);

        // 解决下滑隐藏dialog 后，再次调用show 方法显示时，不能弹出Dialog
//        mDialogBehavior = BottomSheetBehavior.from(findViewById(R.id.design_bottom_sheet));
        mDialogBehavior = BottomSheetBehavior.from((View) rootView.getParent());
//        mDialogBehavior.setPeekHeight(getWindowHeight());
        mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        mDialogBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int newState) {
                //监听BottomSheet状态的改变
                if (newState == BottomSheetBehavior.STATE_HIDDEN || newState == BottomSheetBehavior.STATE_COLLAPSED) {
//                    mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    BaseBottomDialog.this.dismiss();
                }
            }

            @Override
            public void onSlide(@NonNull View view, float v) {
                //监听拖拽中的回调，根据slideOffset可以做一些动画
            }
        });

    }

    protected abstract int contentViewId();

    /**
     * 设置dialog位于屏幕底部
     */
    public void setViewLocation(){
        getWindow().setGravity(Gravity.BOTTOM);//设置显示在底部
        WindowManager windowManager=getWindow().getWindowManager();
        Display display= windowManager.getDefaultDisplay();
        WindowManager.LayoutParams layoutParams=getWindow().getAttributes();
        layoutParams.width=display.getWidth();//设置Dialog的宽度为屏幕宽度
//        layoutParams.height = display.getHeight();
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(layoutParams);
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if(mDialogBehavior != null){
            mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

}
